Introduction

The aim of this project is to develop a machine learning model that predicts the loan status of customers based on information provided in their application profile. It is a binary classification problem (a binary response) in which we predict whether a loan would be approved or not. I will be using open-source data pulled from Kaggle (pulled from an Analytics Vidhya Hackathon and implementing multiple techniques to yield the most accurate model for the problem.

Problem Statement

Loans are a necessity of the modern world, supporting consumption, economic growth, and business operations. Many types loans exist for different purposes across various stages of life, among which are home loans, which we intend to tackle in this problem.

Dream Housing Finance company deals in all home loans. They have a presence across all urban, semi-urban and rural areas. Customers can apply for a home loan after the company validates their eligibility. The company wants to automate the loan eligibility process (real-time) based on customer detail provided in their application. The company wants to identify customer segments that are eligible for loan amounts so that they can specifically target these customers.

Loan prediction is a very common real-life problem that every retail bank faces at least once; automating this process could save time, resources, and money. There is, however, an unambiguously bias in lending. As such, we seek to examine the factors most predictive of loan status and fit multiple models to automate loan eligibility.

Dataset description

The data files provided consists of a training set (train.csv) and test set (test.csv), which contains similar data-points as train except for the loan status to be predicted. The training set consists of 614 observations on 13 variables (8 categorical and 5 numeric); the testing consists of 367 observations on 12.

Since this project employs supervised learning methods, I intend to use only the training set. The dataset will be split into 70% training and 30% testing, using observed values to evaluate predictive accuracy.

Project outline

First, I will load the data, perform initial data manipulation and cleaning, and address missing values. Next, I will perform exploratory data analysis, employing visualization and inferential techniques to identify trends, patterns, and relationships. After examining the data, I will perform some final tidying before setting up the models. I will split the train.csv into a train and test set (70/30), build a recipe, and create validation sets to generate multiple estimations of the test error rate. We then fit 6 supervised learning models (logistic, LDA, QDA, elastic net, KNN, and pruned decision trees) before assessing their performance using several evaluation metrics. From there, we will select the best model and fit it to our testing data.

Exploratory Data Analysis

In this section, we will first load the data, perform some data tidying and transformation, examine missing values, and then perform some preliminary analysis on the dataset.

Loading and exploring the data

First, let’s take a look at our data.

loan_ds <- read.csv("~/Desktop/School/PSTAT/PSTAT 131/proj-final/project_data/train.csv")
str(loan_ds)
## 'data.frame':    614 obs. of  13 variables:
##  $ Loan_ID          : chr  "LP001002" "LP001003" "LP001005" "LP001006" ...
##  $ Gender           : chr  "Male" "Male" "Male" "Male" ...
##  $ Married          : chr  "No" "Yes" "Yes" "Yes" ...
##  $ Dependents       : chr  "0" "1" "0" "0" ...
##  $ Education        : chr  "Graduate" "Graduate" "Graduate" "Not Graduate" ...
##  $ Self_Employed    : chr  "No" "No" "Yes" "No" ...
##  $ ApplicantIncome  : int  5849 4583 3000 2583 6000 5417 2333 3036 4006 12841 ...
##  $ CoapplicantIncome: num  0 1508 0 2358 0 ...
##  $ LoanAmount       : int  NA 128 66 120 141 267 95 158 168 349 ...
##  $ Loan_Amount_Term : int  360 360 360 360 360 360 360 360 360 360 ...
##  $ Credit_History   : int  1 1 1 1 1 1 1 0 1 1 ...
##  $ Property_Area    : chr  "Urban" "Rural" "Urban" "Urban" ...
##  $ Loan_Status      : chr  "Y" "N" "Y" "Y" ...

A few caveats:

  • Dependents, a numeric variable, is encoded as a factor.
  • Credit_History, a categorical variable, is encoded as numeric.
  • ApplicantIncome and CoapplicantIncome are given as monthly (instead of annual) figures in dollar amounts, while LoanAmount is a lump-sum given in terms of thousands.


Now, let’s look at missing values:

colSums(is.na(loan_ds))  
##           Loan_ID            Gender           Married        Dependents 
##                 0                 0                 0                 0 
##         Education     Self_Employed   ApplicantIncome CoapplicantIncome 
##                 0                 0                 0                 0 
##        LoanAmount  Loan_Amount_Term    Credit_History     Property_Area 
##                22                14                50                 0 
##       Loan_Status 
##                 0

We can see that there are NULLS in LoanAmount, Loan_Amount_term and Credit_History. It is important to note however that is.na() does not detect blank cells in character variables. Thus we need to employ a different methodology to identify empty strings.

sapply(loan_ds,function(x) table(as.character(x) =="")["TRUE"])
##           Loan_ID.NA          Gender.TRUE         Married.TRUE 
##                   NA                   13                    3 
##      Dependents.TRUE         Education.NA   Self_Employed.TRUE 
##                   15                   NA                   32 
##   ApplicantIncome.NA CoapplicantIncome.NA        LoanAmount.NA 
##                   NA                   NA                   NA 
##  Loan_Amount_Term.NA    Credit_History.NA     Property_Area.NA 
##                   NA                   NA                   NA 
##       Loan_Status.NA 
##                   NA

Indeed, we have blank entries in Gender, Married, Dependents, and Self-employed. We will first transform these blanks into NA’s for easy identification, and then determine what to do about them.

Tidying the data

Transform empty strings in to “NA”.

loan_ds <- read.csv(file="~/Desktop/School/PSTAT/PSTAT 131/proj-final/project_data/train.csv",
                    header=TRUE,na.strings = c("","NA")) 

Scale ApplicantIncome and CoapplicantIncome to match the format of LoanAmount.

loan_ds$ApplicantIncome <- (loan_ds$ApplicantIncome*12)/1000
loan_ds$CoapplicantIncome <- (loan_ds$CoapplicantIncome*12)/1000
  # scale into annual figures
  # convert them into the same units as LoanAmount (in terms of thousands)

Because Loan_ID is unique and not relevant to our analysis, we will remove it.

loan_ds <- loan_ds[,-1]; colnames(loan_ds)
##  [1] "Gender"            "Married"           "Dependents"       
##  [4] "Education"         "Self_Employed"     "ApplicantIncome"  
##  [7] "CoapplicantIncome" "LoanAmount"        "Loan_Amount_Term" 
## [10] "Credit_History"    "Property_Area"     "Loan_Status"

Transform variables

For ease of analysis, we will convert categorical variables into factors and convert Dependents into a numeric variable.

# convert factors
loan_ds$Gender <- factor(loan_ds$Gender, levels = c("Male","Female"))
loan_ds$Married <- factor(loan_ds$Married, levels = c("Yes","No"))
loan_ds$Education <- factor(loan_ds$Education, levels = c("Graduate","Not Graduate"))
loan_ds$Self_Employed <- factor(loan_ds$Self_Employed, levels = c("Yes","No"))
loan_ds$Property_Area <- factor(loan_ds$Property_Area, levels = c("Rural","Semiurban","Urban"))
loan_ds$Loan_Status <- factor(loan_ds$Loan_Status, levels = c("Y","N"), labels = c("Yes","No")) 

# convert credit history into factor
loan_ds$Credit_History <- factor(loan_ds$Credit_History, levels = c(1,0), labels = c("Yes","No"))  

# convert Dependents into numeric 
loan_ds$Dependents <- recode(loan_ds$Dependents,"3+"="3") %>%
  as.integer(loan_ds$Dependents)

Missing data

Let’s start by visualizing the missing data.

vis_miss(loan_ds) # visualize missing data

It looks like the majority of our variables contain missing values. Let’s produce a summary of the missing values to view the percentage of missingness in each variable, as well as a cumulative sum.

loan_ds %>%
  miss_var_summary(add_cumsum = TRUE)

There are 149 missing values in our dataset; with Credit_History comprising the largest proportion. The simplest solution is to remove variables with a lot of missing values. However, removing variables at this early of a stage is inappropriate since we do not yet know which variables will be significant. As a general rule, we do not want to remove more than 10% of the overall dataset. We will likely need to perform imputation at a later step.

Let’s continue exploring our data and return to this issue later.

Variable description

The dataset consists of the following variables:

  • Loan_ID : Unique Loan ID.
  • Gender : Male / Female.
  • Married : Whether the applicant is married (Yes/No)
  • Dependents : The number of dependents (1,2,3) an applicant has.
  • Education : An applicant’s education level (Graduate/Under Graduate)
  • Self_Employed : Whether the applicant is self-employed (Yes/No)
  • ApplicantIncome : An applicant’s annual income (in thousands of dollars).
  • CoapplicantIncome : A coapplicant’s annual income (in thousands of dollars)
  • LoanAmount : The loan amount requested by an applicant (in thousands of dollars).
  • Loan_Amount_Term : The term of the loan in months.
  • Credit_History : Does the applicant’s credit history meet the bank’s requirements (Yes/No)?
  • Property_Area : An applicant’s area of residence (Urban/Semi Urban/Rural).
  • Loan_Status : Whether the loan was approved (Yes/No). This is the target variable.

Visual EDA

This section consists of data exploration and visualization before running our predictive models. We will first look at the response, generate a correlation matrix, and then examine the independent variables one by one to identify potential relationships (and confounds).

Loan status

First, we will look at the distribution of the response by creating a barplot.

loan_ds %>% 
  ggplot(aes(x = Loan_Status)) +
  geom_bar() + 
  theme_grey()

loan_ds %>%
  select(Loan_Status) %>%
  table() %>%
  prop.table() 
## Loan_Status
##       Yes        No 
## 0.6872964 0.3127036

Approximately 69% of applicants were approved for loan while 31% were rejected. This imbalance in factor levels may make it difficult for the model to learn to predict Loan_Status accurately, so we’ll need to upsample or downsample the data at a later step.

Correlation plot

We first create a heatmap of the numeric variables to get an idea of their relationship. The corrplot() function generates a correlation matrix, where the main diagonal represents the variance of each variable while the other cells represent covariances The color legend indicates the strength and direction of the relationship for each pair. The plot below shows a moderate, positive correlation between LoanAmount and ApplicantIncome, and very little correlation (+/- 0.20) among the other predictors.

Below, we can see that LoanAmount and ApplicantIncome have a correlation coefficient of 0.57. Keep this in mind as we explore the dataset.

loan_ds %>%
  select(where(is.numeric)) %>%
  na.omit() %>%  
  cor() %>%
  corrplot(method="number")

Now, we will analyze our predictors one-by-one to examine their distribution and relationship with the response.

Loan Amount

We see that loan amount is right (positively) skewed, with most values between 0-400 thousand. As a result, extremely large outliers may pull the mean upwards. The mean loan amount requested by approved applicants and rejected applicants is about the same. However, there is more variation among the rejected applicants.

require(gridExtra)

plot1 <- loan_ds %>%
  na.omit(LoanAmount) %>%
  ggplot(aes(x=LoanAmount)) + 
  geom_histogram(bins=40) +
  theme_grey()

plot2 <- loan_ds %>%
  na.omit(LoanAmount) %>%
  ggplot(aes(Loan_Status, LoanAmount)) + 
  geom_boxplot(na.rm=T) +
  geom_jitter(alpha = 0.1) +
  theme_grey()

grid.arrange(plot1, plot2, ncol=2)

Recall that loan amount is positively correlated with applicant income. The first plot shows a slight linear trend between the two variables, especially in the lower amounts on the left. The second plot shows a mix of approved and rejected applicants in each income range. Though it looks like higher income applicants do tend to apply for higher loans, it is difficult to determine whether having a higher income improves their chances of getting approved. We should take a closer look at Applicant Income.

Applicant Income

We can see from the plot that applicant incomes are right skewed, with most values between 0-300 thousand. Even with the extremely high outliers omitted, there’s a disrepency between the proportions of the income ranges. Average incomes among approved and rejected applicants are about the same.

plot1 <- loan_ds %>%
  filter(ApplicantIncome < 500) %>%  
    # omit high outliers for ease of visualization
  ggplot(aes(x=ApplicantIncome)) + 
  geom_histogram(fill="bisque",color="white",alpha=0.7, bins=20) + 
  geom_density() +
  geom_rug() + 
  labs(x = "applicant income") +
  theme_minimal()

plot2 <- loan_ds %>%
  filter(ApplicantIncome < 500) %>%  
  ggplot(aes(y=ApplicantIncome,x=Loan_Status, color=Loan_Status))+
  geom_boxplot() +
  theme_grey()

grid.arrange(plot1, plot2, ncol=2)

Upon inspecting the outliers (ApplicantIncome > 500), we find that: 1) 2 out of 3 applicants have dependents, 2) 3 out of 3 are graduates, and 3) 3 out of 3 have no coapplicant. Assuming coapplicants are spouses, those with dependents are likely single fathers. The only rejected applicant 1) lives in a rural area, and 2) has bad credit history. From these observations, we may infer that dependents don’t seem to affect loan eligibility much, neither does loan amount or term. Let’s keep this in mind.

loan_ds %>%
  filter(ApplicantIncome > 500)
Let’s take a closer look at proportions. Applicants with incomes in the 400-500 range have the highest approval rate of 75%. So, higher income do somewhat translate to a higher approval rate. But since there are so few data points in the (400,500] range, this may just be random variation.

Coapplicant Income

Let’s start by taking a look at its density, grouped by loan status. Both plots show that coapplicant incomes are right skewed, with a mix of high and low incomes in each loan status category. Average coapplicant income for approved applicants is slightly higher than that of rejected applicants.

loan_ds %>%
  ggplot(aes(x=Loan_Status, y=CoapplicantIncome, color=Loan_Status)) + 
  geom_boxplot()

An interesting finding is that a lot of coapplicant incomes are 0 (ie., no coapplicant) in both the Yes and No category. I was confident that having a high coapplicnat income would boost approval rates.

Let’s explore this further. The table below shows that 273 out of 614 records have a coapplicant income of 0, or about 44%. This is pretty large given the size of our dataset.

loan_ds %>%
  dplyr :: count(CoapplicantIncome == 0) 

A natural question we may ask is how the presence of a coapplicant alone (not the numerical value of their income) affects the chances that a given applicant will be approved. The variable has_coapp has the value of FALSE if coapplicant income is 0, and TRUE otherwise. From the contingency table, we see that about 72% of applicants with a coapplicants get approved, while only ~65% of applicants without a coapplicant get approved. That’s a 7% difference!

loan_ds %>%
  dplyr:: mutate(has_coapp = if_else(CoapplicantIncome != 0,TRUE,FALSE)) %>%
  group_by(has_coapp, Loan_Status) %>% 
  dplyr:: summarise(n=n()) %>%
  dplyr::mutate(freq = prop.table(n))  
# on average, ~72% of applicants w/ a coapplicant were approved for a loan
# while only ~65% of applicants w/o a coapplicant were approved.

Perhaps the presense of a coapplicant is more predictive of loan status than the numerical value of their income. We should consider transforming CoapplicantIncome into a factor.

Loan amount term

Loan amout term is the term of the loan in months. From the plot below, we see that it has a left skew. This means that its mean (342 months ie., 28.5 years) is lower than its median and mode (both; 360 months ie., 30 years). Because there are so few points on the lower end, the mode is more representative of the center.

loan_ds %>%
  na.omit(Loan_Amount_Term) %>%
  ggplot(aes(x=Loan_Amount_Term)) + 
  geom_bar() +
  theme_grey()

# mfv 360 

We now look at Loan_Amount_Term in relation to Loan_Status Applicants requesting short-term loans seem more likely to qualify, on average, but we do see a high approval rate for 360. We should also keep in mind that ~85% of loans have a term of 360, however, so the data may be underrepresenting applicants requesting alternative loan terms.

loan_ds %>%
  na.omit(Loan_Amount_Term) %>%  
  ggplot(aes(x=Loan_Amount_Term, fill=Loan_Status)) + 
  geom_bar(position="fill")

Dependents

Dependents is right skewed; most applicants have no dependents. Approval rates are relatively similar across each number of dependents, with the highest approval rate for 2. There is no clear pattern; this indicates that an applicant’s Dependents may not be influential in determining their Loan_Status.

plot1<-loan_ds %>%
  na.omit(Dependents) %>%  # should be able to impute this later
  ggplot(aes(x=Dependents)) +
  geom_bar()
# most applicants have no dependents

# dependents vs. loan status
plot2<-loan_ds %>%
  na.omit(Dependents) %>%  
  ggplot(aes(x=Dependents, fill=Loan_Status)) + 
  geom_bar(position="fill")
# relatively similar likelihood of approval for each # of dependents

grid.arrange(plot1,plot2,ncol=2)

Do applicants with dependents request a larger loan than those without? Indeed, we can see from the boxplots that individuals with dependents do, on average, request a larger loan amount!

Gender

One thing to note is that there are more male applicants than female applicants (81% vs 19%); thus females may be under represented. A natural question to ask is whether there is bias. From the plots below, we can see that females are indeed less likely (about 8%) to be approved for a loan than males.

prop.table(table(loan_ds$Gender))
## 
##      Male    Female 
## 0.8136439 0.1863561
# much more males than females.. unrepresentative? 

# is there bias in the selection process? 
loan_ds %>%
  na.omit(Gender) %>%  # should be able to impute this later
  ggplot(aes(x=Gender, fill=Loan_Status)) + 
  geom_bar(position="fill")

# 2-way contingency table 
loan_ds %>%
  na.omit(Gender) %>%
  group_by(Gender, Loan_Status) %>% 
  dplyr:: summarise(n=n()) %>%
  dplyr::mutate(freq = prop.table(n))  
# slight bias; females 8% less likely to be approved than males

Married

Surprisingly, given that most applicants have no dependents, married individuals comprise a majority of our dataset (~60%). Nonetheless, a 60-40 ratio offers a good contrast. Married individuals 10% more likely to be approved for a loan. Given the size of our dataset, a 10% difference is pretty significant!

# distribution
prop.table(table(loan_ds$Married))
## 
##       Yes        No 
## 0.6513912 0.3486088
# are married individuals more likely to be approved?
loan_ds %>%
  na.omit(Married) %>%
  ggplot(aes(x=Married,fill=Loan_Status)) +
  geom_bar(position="fill")

# 2-way contingency table 
loan_ds %>%
  na.omit(Married) %>%
  group_by(Married, Loan_Status) %>% 
  dplyr:: summarise(n=n()) %>%
  dplyr::mutate(freq = prop.table(n))  
# given the size of our dataset, 10% is pretty large! 

Education

Education denotes an applicant’s educational attainment; Graduate or Not Graduate. This may be an oversimplification of the possible levels of education that can be attained; however, a simple encoding is just what we need. Our dataset is comprised of ~80% graduates, which makes sense due to educational loans. An 80-20 ratio, however, is definitely unbalanced. From the barplots, we can see that graduates are more likely to get approved (8% more).

prop.table(table(loan_ds$Education))
## 
##     Graduate Not Graduate 
##     0.781759     0.218241
# our data is comprised of ~80% graduates!
# makes sense b/c educational loans, etc. 

loan_ds %>%
  na.omit(Education) %>%
  ggplot(aes(x=Education,fill=Loan_Status)) +
  geom_bar(position="fill")

# graduates are slightly more likely to get approved 

# 2-way contingency table 
loan_ds %>%
  na.omit(Education) %>%
  group_by(Education, Loan_Status) %>% 
  dplyr:: summarise(n=n()) %>%
  dplyr::mutate(freq = prop.table(n))  
# graduates are about 8% more likely 

Self employed

Self-employed is a categorical variable that indicates if an applicant is self-employed. Only 14% of applicants in the dataset are self-employed. There is no significant difference in the approval rates between self-employed and non self-employed individuals; a slight difference of 4%, with non self-employed individuals having the higher rate.

prop.table(table(loan_ds$Self_Employed))
## 
##       Yes        No 
## 0.1408935 0.8591065
loan_ds %>%
  na.omit(Self_Employed) %>%
  ggplot(aes(x=Self_Employed,fill=Loan_Status)) +
  geom_bar(position="fill")

# 2-way contingency table 
loan_ds %>%
  na.omit(Self_Employed) %>%
  group_by(Self_Employed, Loan_Status) %>% 
  dplyr:: summarise(n=n()) %>%
  dplyr::mutate(freq = prop.table(n))  
# slight difference; not self-empoyed 4% more likely to be approved

Credit History

Credit history is a categorical variable that indicates if an applicant’s credit history satisfies the bank’s requirements (ie., if they have good credit history). Most applicants do have good credit history (~85%; unbalanced). There may be some selection bias, though, since individuals with good credit history may be more inclined to apply in the first place. Unsurprisingly, credit history turns out to be a very important predictor for loan status, given that nearly 80% of applicants with good credit history get approved, whereas only 10% of applicants with bad credit history do.

prop.table(table(loan_ds$Credit_History))
## 
##       Yes        No 
## 0.8421986 0.1578014
# most applicants have good credit history! 

loan_ds %>%
  na.omit(Credit_History) %>%
  ggplot(aes(x=Credit_History,fill=Loan_Status)) +
  geom_bar(position="fill")

# 2-way contingency table 
loan_ds %>%
  na.omit(Credit_History) %>%
  group_by(Credit_History, Loan_Status) %>% 
  dplyr:: summarise(n=n()) %>%
  dplyr::mutate(freq = prop.table(n))  
# *** VERY important predictor!!
# about 70% more likely to be approved if yes! 
# only 10% of applicants w/ bad credit history get approved

Property Area

Property area is a categorical variable that indicates the area in which an applicant resides (Urban, Semi Urban, Rural). We have a pretty good mix of applicants from all 3 areas. Semiurban has the highest approval rate, then urban, then rural. With respect to other predictors, we can see that married individuals tend to prefer semiurban areas over urban or rural areas. This is a pretty insightful finding because, as we recall, married individuals had a 10% higher approval rate than non-married individuals. Coupled with a higher approval rate for those with coapplicants, we may just find our target demographic!

prop.table(table(loan_ds$Property_Area))
## 
##     Rural Semiurban     Urban 
## 0.2915309 0.3794788 0.3289902
# good mix of applicants from all 3 areas

loan_ds %>%
  na.omit(Property_Area) %>%
  ggplot(aes(x=Property_Area,fill=Loan_Status)) +
  geom_bar(position="fill")

# 2-way contingency table 
loan_ds %>%
  na.omit(Property_Area) %>%
  group_by(Property_Area, Loan_Status) %>% 
  dplyr:: summarise(n=n()) %>%
  dplyr::mutate(freq = prop.table(n))  
# is property area related to any other predictors?
loan_ds %>%
  na.omit(Property_Area, Loan_Status, Married) %>%
  dplyr:: mutate(has_coapp = if_else(CoapplicantIncome != 0,TRUE,FALSE)) %>%
  ggplot(aes(x=Property_Area,fill=Married)) +
  geom_bar(position="dodge") +
  facet_wrap(~has_coapp)

Final tidying

Now that we’ve explored the dataset, we will need to fix some errors before continuing with our analysis. Let’s review these issues:

  • There are missing values in Credit_History, Self_Employed, LoanAmount, Dependents, Loan_Amount_Term, Gender, and Married. Based on the type of variable, we will determine which method to use.
  • Looking at the distributions of the data, we see that ApplicantIncome and LoanAmount have some extreme (high) outliers.

MICE

Missingness exists in both numerical and categorical data. Therefore, we will be using the mice package. The MICE (Multivariate Imputation by Chained Equations) algorithm imputes missing values with plausible data values inferred from other variables in the dataset.

# install and load 
# install.packages("mice")
library(mice)

From the missing data table below, we see that the first two variables are missing a large proportion of its values, while the latter five are missing some.

loan_ds %>%
  miss_var_summary()

Now, we call the mice package. The argument m indicates the number of multiple imputations; the standard is m=5. The method argument specifies the imputation method applied to all variables in the dataset; a separate method can also be specified for each variable.

We can control the defaultMethod used for 1) numeric data, 2) categorical data with 2 levels, 3) categorical data with >2 unordered levels, and 4) factor data with >2 ordered levels. I will choose predictive mean matching for numeric data, logistic regression for 2-level factors, linear discriminant analysis for unordered factor data, and proportional odds for ordered factor data.

imp <- mice(loan_ds, m=5, defautMethod = c("pmm","logreg", "lda", "polr"))

Here, we can see the actual imputations for Dependents:

imp$imp$Dependents

Now let’s merge the imputed data into our original dataset via the complete() function.

loan_ds <- complete(imp,5)  # I chose the 5th round of data imputation

Check missing data again, we note that there is no missing data after the imputation:

loan_ds %>%
  miss_var_summary()

Outliers

Outliers can be tricky. It’s hard to determine if they are data entry errors, sampling errors, or natural variation in our data. If we decide to remove records, however, it may result in information loss. We will assume that the missing values are systematic until proven otherwise.

Looking at LoanAmount, we see that the “extreme” values are somewhat plausible. Some customers may want to apply for a loan as high as 650 thousand.

zscore <- (abs(loan_ds$LoanAmount-mean(loan_ds$LoanAmount, na.rm=T))/sd(loan_ds$LoanAmount, na.rm=T))
loan_ds$LoanAmount[which(zscore > 3)]
##  [1] 436 650 600 700 495 436 480 490 570 405 500 480 480 600 496

Since we have a positive skew, we will perform a log transformation to normalize the data. Now the data looks closer to normal and the effect of extreme outliers are significantly smaller.

loan_ds$LogLoanAmount <- log(loan_ds$LoanAmount)
plot1 <- loan_ds %>% 
  ggplot(aes(x=LoanAmount)) +
  geom_histogram(bins=20) +
  geom_density()+
  labs(title="Histogram for Loan Amount") +
  xlab("Loan Amount") 

plot2 <- loan_ds %>% 
  ggplot(aes(x=LogLoanAmount)) +
  geom_histogram(bins=20) +
  geom_density()+
  labs(title="Histogram for Log Loan Amount") +
  xlab("Log Loan Amount") 

grid.arrange(plot1,plot2,ncol=2)

As for ApplicantIncome, we also have a pretty severe positive skew, so we will perform a log transformation. The data looks much better.

loan_ds$LogApplicantIncome <- log(loan_ds$ApplicantIncome)
plot1 <- loan_ds %>% 
  ggplot(aes(x=ApplicantIncome)) +
  geom_histogram(bins=20) +
  geom_density()+
  labs(title="Histogram for Applicant Income") +
  xlab("Applicant Income") 

plot2 <- loan_ds %>% 
  ggplot(aes(x=LogApplicantIncome)) +
  geom_histogram(bins=20) +
  geom_density()+
  labs(title="Histogram for Log Applicant Income") +
  xlab("Log Applicant Income") 

grid.arrange(plot1,plot2,ncol=2)

Now, we will remove the original variables from our dataset

loan_ds <- select(loan_ds,-LoanAmount)  # remove original variable
loan_ds <- select(loan_ds,-ApplicantIncome)  # remove original variable 

Setting Up Models

Now that we have a better idea of how the variables in our dataset impact loan status, it’s time to set up our models. We will perform our train/test split, create our recipe, then establish 10-fold cross-validation to help with our models.

Train/test split

Before we do any modeling, we will need to (randomly) split our dataset into training / testing data. The reason why we split our data is to avoid overfitting; we will fit the models on the training data, then use those models to make predictions on the previously unseen testing data. The testing set is reserved to be fit only once after the models have “learned” from the training set. From there, we can use error metrics to evaluate each model’s performance. We will use a 70/30 split since our dataset is relatively small and we want to reserve enough data for the testing set. We will set a random seed before our split so that we can replicate our results, and stratify on our response.

set.seed(3450)
loan_split <- initial_split(loan_ds, prop = 0.70, 
                              strata = "Loan_Status")

loan_train <- training(loan_split)
loan_test <- testing(loan_split)
loan_folds <- vfold_cv(loan_train, v = 10, strata = "Loan_Status")

Dimensions of our datasets:

dim(loan_train); dim(loan_test)
## [1] 429  12
## [1] 185  12

Building the recipe

Now that we’ve completed all the premilminary steps, it’s time to build our recipe. Think of it as following a recipe for cut-out cookies. Because we’ll be using a variety of different molds (models), each cookie will look different, but their ingredients will be the same! Inside, they’re all the same flour and sugar and eggs! That’s what this recipe is; a unique mix of ingredients that will be fitted to different molds. Our goal turns into finding the best mold for our particular mix. From there, fitting the best model to our test data is analogous to using a different brand of the essential ingredients (ie., the test data), shaping the dough with our best cookie mold, then putting it into the oven!

In our recipe, we’ll be using 8 out of the 11 original predictors, 2 transformed variables LogLoanAmount and LogApplicantIncome, plus a new variable Coapplicant.

We’ll first need to upsample the data. Recall from earlier that our response was severely imbalanced; if we train our models on an imbalanced dataset, they can accidently become better at identifying one level versus another, which is undesirable. Two solutions come to mind: upsampling or downsampling. Since we have a small dataset, step_upsample() is the better option. We’ll use over_ratio=1 so that are equally as many Yes’s as there are No’s. Because upsampling is intended to be performed on the training set alone, the default skip option is skip=TRUE. We’ll use skip=FALSE to make sure that it’s brought the counts to be equal and then rewrite the recipe without.

Since the numerical values of CoapplicantIncome are too not related to our response, we’ll transform it into a categorical variable Coappliant instead to indicate the presence / absence of a coapplicant. We’ll then scale and center our numeric predictors, and dummy-code the nominal predictors.

loan_recipe <- recipe(Loan_Status~., data=loan_train) %>%
  step_upsample(Loan_Status, over_ratio = 1, skip = FALSE) %>% 
  step_mutate(Coapplicant = factor(if_else(CoapplicantIncome!=0, "Yes","No",NA))) %>%
  step_rm(CoapplicantIncome)  %>% 
    # transform coapplicant income into a factor
    # Yes if CoapplicantIncome is not 0, No otherwise. 
  step_scale(all_numeric_predictors()) %>%
  step_center(all_numeric_predictors()) %>%  # scale and center
  step_dummy(all_nominal_predictors())    # convert into factor 
prep(loan_recipe) %>% bake(new_data = loan_train) %>% 
  group_by(Loan_Status) %>% 
  dplyr :: summarise(count = n())

Now we rewrite the recipe with skip=TRUE:

loan_recipe <- recipe(Loan_Status~., data=loan_train) %>%
  step_upsample(Loan_Status, over_ratio = 1, skip = TRUE) %>% 
  step_mutate(Coapplicant = factor(if_else(CoapplicantIncome!=0, "Yes","No",NA))) %>%
  step_rm(CoapplicantIncome)  %>% 
    # transform coapplicant income into a factor
    # Yes if CoapplicantIncome is not 0, No otherwise. 
  step_scale(all_numeric_predictors()) %>%
  step_center(all_numeric_predictors()) %>%  # scale and center
  step_dummy(all_nominal_predictors())    # convert into factor 

We can use prep() to check the recipe to verify it worked.

prep(loan_recipe) %>% 
  bake(new_data = loan_train) %>% 
  kable() %>% 
  kable_styling(full_width = F) %>% 
  scroll_box(width = "100%", height = "200px")
Dependents Loan_Amount_Term LogLoanAmount LogApplicantIncome Loan_Status Gender_Female Married_No Education_Not.Graduate Self_Employed_No Credit_History_No Property_Area_Semiurban Property_Area_Urban Coapplicant_Yes
0.2953466 0.2368733 -0.0431027 0.1280076 No 0 0 0 1 0 0 0 1
2.3215618 0.2368733 0.3674913 -0.4959078 No 0 0 0 1 1 1 0 1
-0.7177610 0.2368733 -0.2689708 -1.2439388 No 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 -1.0596137 -0.2761123 No 1 1 0 1 1 0 1 0
-0.7177610 0.2368733 -0.4479923 0.9062231 No 0 0 1 1 1 0 1 0
-0.7177610 0.2368733 -0.2350575 -0.7307845 No 0 0 1 1 1 1 0 1
0.2953466 0.2368733 0.2791282 -0.1892989 No 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -2.5715881 -1.6238742 No 0 1 1 1 0 0 1 0
0.2953466 0.2368733 0.8368672 -0.0165239 No 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 -1.1116158 -0.4319064 No 0 1 0 1 0 0 1 0
2.3215618 0.2368733 1.7436325 1.6481662 No 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 -0.5244713 -1.2645184 No 0 0 0 1 1 0 1 1
-0.7177610 0.2368733 -0.9595935 -0.2377547 No 0 1 0 1 0 0 1 0
-0.7177610 0.2368733 -0.1689506 -0.7047869 No 1 0 0 1 1 0 1 1
-0.7177610 0.2368733 -0.3034843 0.0065739 No 1 1 0 1 0 1 0 0
1.3084542 0.2368733 0.0462243 0.1388776 No 0 0 0 1 0 0 1 0
0.2953466 0.2368733 1.5245944 1.5218393 No 1 0 0 0 1 0 1 0
0.2953466 0.2368733 -0.5244713 0.2431861 No 0 0 0 1 1 0 0 0
-0.7177610 0.2368733 -0.2350575 -0.0165239 No 1 1 0 1 1 1 0 0
-0.7177610 0.2368733 1.3236852 0.4653528 No 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.0751132 0.0314404 No 1 1 0 1 1 1 0 0
-0.7177610 0.2368733 0.5778710 -0.1759075 No 0 1 0 1 0 0 1 1
0.2953466 0.2368733 -0.3386198 -2.1784279 No 0 0 0 0 0 0 1 1
-0.7177610 0.2368733 0.0316177 0.7217816 No 0 1 0 0 0 0 1 0
-0.7177610 0.2368733 -0.1689506 0.8401258 No 0 1 1 1 0 0 0 0
1.3084542 0.2368733 0.9772130 -0.1558404 No 0 0 0 1 1 0 1 1
-0.7177610 0.2368733 0.2919993 -0.2495844 No 0 0 1 1 1 0 0 1
-0.7177610 0.2368733 0.5667601 0.4229599 No 0 0 0 1 0 0 0 1
2.3215618 0.2368733 1.5516777 -0.0781287 No 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.8644544 0.1280076 No 1 0 0 1 0 0 0 0
-0.7177610 0.2368733 1.1988448 1.9242907 No 0 1 0 1 1 1 0 0
1.3084542 0.2368733 -0.0279278 -0.0042094 No 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 -0.2017239 0.6045475 No 0 1 0 1 1 0 0 0
-0.7177610 0.2368733 1.2148939 1.1781572 No 0 1 0 1 0 0 1 0
-0.7177610 0.2368733 1.4481154 0.9592790 No 0 0 0 1 0 0 1 0
-0.7177610 2.2220335 -1.4254245 -0.9586106 No 0 1 0 1 1 1 0 0
2.3215618 -2.7408669 -0.9353700 -0.2709415 No 0 0 1 1 0 0 0 0
-0.7177610 2.2220335 -0.4858569 -0.8949495 No 0 1 1 1 1 0 1 1
-0.7177610 0.2368733 -1.3053887 -1.1932851 No 0 1 0 1 1 0 0 1
0.2953466 -0.7557067 -1.3347121 -0.9991058 No 0 0 0 0 1 0 0 0
-0.7177610 0.2368733 0.4401692 1.5863190 No 0 1 0 1 0 1 0 0
-0.7177610 0.2368733 0.3920195 0.5444794 No 0 0 1 1 0 0 1 0
-0.7177610 0.2368733 -0.8878076 1.4544928 No 0 1 0 0 0 0 1 0
2.3215618 -2.7408669 -0.3386198 -0.0811618 No 0 0 1 1 0 0 1 0
-0.7177610 0.2368733 -0.0583967 -0.3163526 No 0 1 0 1 0 1 0 0
2.3215618 0.2368733 -0.0431027 -0.3110429 No 0 0 0 1 1 1 0 1
-0.7177610 0.2368733 0.0607222 -5.0526513 No 0 0 0 1 0 0 0 1
1.3084542 0.2368733 -1.1650429 0.2599440 No 0 0 0 1 1 1 0 0
-0.7177610 0.2368733 -1.5205636 -0.9694858 No 0 0 0 1 1 0 1 0
-0.7177610 0.2368733 0.1035803 0.1774403 No 0 0 1 0 0 0 1 0
-0.7177610 0.2368733 1.3008783 0.4706354 No 0 0 0 1 1 0 1 0
0.2953466 -2.7408669 0.1316379 -0.7841576 No 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 0.8368672 -1.4032922 No 0 0 1 1 1 1 0 1
1.3084542 0.2368733 0.1035803 -0.3123686 No 1 1 0 1 0 0 1 0
-0.7177610 0.2368733 0.7475402 2.0456601 No 0 1 0 0 1 0 1 0
-0.7177610 0.2368733 -0.2519404 0.5474899 No 0 1 1 1 1 0 0 0
2.3215618 0.2368733 -0.6451262 0.3338633 No 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 1.6436123 1.8920306 No 0 0 0 1 0 0 0 1
2.3215618 0.2368733 -0.0431027 0.2388907 No 0 0 1 1 1 1 0 0
-0.7177610 0.2368733 -0.8185707 2.4099457 No 0 0 0 1 0 0 0 0
-0.7177610 -0.7557067 -0.5440691 -0.7307845 No 1 1 0 0 0 1 0 1
1.3084542 0.2368733 -0.0583967 0.0662706 No 0 0 0 1 1 1 0 0
1.3084542 2.2220335 0.1729814 0.3813055 No 1 1 0 1 1 0 1 0
0.2953466 0.2368733 -0.2861512 -1.1177099 No 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 -0.6659817 -1.2879043 No 0 0 1 1 1 0 1 1
-0.7177610 0.2368733 2.5342754 2.3777914 No 0 1 0 1 0 0 0 0
-0.7177610 0.2368733 0.3797939 -0.8066571 No 0 0 0 1 0 0 0 1
0.2953466 0.2368733 2.0580998 1.0931727 No 0 0 0 0 1 0 0 1
-0.7177610 0.2368733 -0.3386198 0.0430231 No 1 0 0 1 0 0 1 1
2.3215618 0.2368733 -0.5244713 -1.2628617 No 1 1 1 1 1 0 1 0
2.3215618 0.2368733 0.5443464 -0.7036417 No 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 -2.0386756 -0.8660048 No 1 1 0 1 0 0 0 0
-0.7177610 0.2368733 1.6691024 1.1077874 No 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 -0.4293322 0.4043432 No 1 0 0 0 1 0 0 0
0.2953466 0.2368733 -1.3053887 -0.1262294 No 0 0 0 1 0 0 1 0
-0.7177610 2.2220335 -0.5244713 -0.5139802 No 0 0 1 1 1 0 1 1
-0.7177610 0.2368733 0.0316177 0.5897523 No 0 1 1 1 0 0 0 0
-0.7177610 2.2220335 -0.6040728 -0.7902057 No 0 1 0 1 0 1 0 0
-0.7177610 0.2368733 2.8693774 2.3396507 No 0 0 0 1 0 0 0 1
0.2953466 -2.7408669 -0.3209729 -0.4829884 No 0 1 0 1 1 0 1 1
1.3084542 0.2368733 -0.5244713 0.1223773 No 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -1.1923156 -0.4257001 No 1 1 0 1 1 0 1 0
-0.7177610 0.2368733 -0.4668327 -1.5641298 No 1 1 0 1 1 1 0 1
1.3084542 -2.7408669 -2.0815337 -0.5711002 No 0 0 1 1 1 0 1 0
-0.7177610 -0.7557067 -1.3644831 -0.6430543 No 0 1 1 1 0 0 0 0
-0.7177610 0.2368733 0.7677473 0.8622766 No 1 1 0 0 0 0 0 0
0.2953466 -0.7557067 0.2919993 1.0277957 No 0 0 0 1 1 1 0 0
-0.7177610 0.2368733 0.9951852 0.5602183 No 0 0 1 1 1 0 0 0
-0.7177610 0.2368733 0.5999048 -1.0194140 No 1 0 1 1 1 1 0 1
0.2953466 0.2368733 0.3920195 -0.7307845 No 1 1 0 1 0 0 1 0
0.2953466 0.2368733 -0.3386198 0.8228797 No 0 0 0 1 1 0 1 1
-0.7177610 0.2368733 -0.4293322 0.6574378 No 0 1 0 1 1 0 0 0
-0.7177610 0.2368733 -1.6902328 -0.2636463 No 0 0 0 0 0 0 0 1
1.3084542 0.2368733 -0.0893491 -0.9106157 No 0 0 1 1 1 0 0 1
-0.7177610 0.2368733 0.2531283 -0.0979534 No 0 0 0 1 1 0 0 1
-0.7177610 0.2368733 0.5443464 -0.1929718 No 0 0 0 1 0 0 1 1
0.2953466 -2.7408669 -0.2861512 -0.7002114 No 0 0 1 1 1 0 0 1
1.3084542 0.2368733 0.0169009 -0.5795142 No 0 0 0 1 1 1 0 1
1.3084542 0.2368733 0.5443464 -0.5409852 No 0 0 0 1 0 0 1 1
0.2953466 0.2368733 2.6138768 2.2283894 No 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -1.0854414 -0.8301180 No 0 1 0 1 0 0 1 0
-0.7177610 0.2368733 0.9129731 0.6059950 No 0 1 0 1 1 0 0 1
0.2953466 0.2368733 0.1035803 -0.0593080 No 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 -0.6040728 -0.2449260 No 0 1 0 1 0 0 1 0
-0.7177610 -2.7408669 -1.2199751 -1.0328686 No 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 1.6880037 0.4887202 No 0 1 0 1 0 0 0 1
1.3084542 0.2368733 -0.1208009 -0.4770625 No 0 0 1 1 1 1 0 1
-0.7177610 0.2368733 0.6751194 0.1607100 No 0 1 0 1 0 1 0 1
-0.7177610 0.2368733 -0.6244915 -0.3243523 No 1 1 1 1 0 0 0 0
-0.7177610 0.2368733 0.3047860 0.1597391 No 0 1 0 1 0 1 0 1
1.3084542 0.2368733 -0.3386198 -0.2428133 No 0 1 0 1 1 0 0 0
0.2953466 0.2368733 0.7877471 1.4885402 No 0 1 0 1 0 1 0 0
-0.7177610 0.2368733 -1.3347121 -0.5564872 No 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 -0.5440691 -1.0271890 No 1 1 0 1 1 1 0 0
1.3084542 0.2368733 0.6960869 0.6881251 No 0 0 0 0 1 0 0 0
-0.7177610 0.2368733 0.6324955 -0.8183421 No 0 0 1 1 1 0 1 1
-0.7177610 -2.7408669 0.0169009 -0.2407035 No 0 0 0 1 1 0 0 1
1.3084542 0.2368733 1.3236852 1.1162480 No 0 0 0 1 1 0 1 0
-0.7177610 0.2368733 -5.2198819 -0.8660048 No 1 1 0 1 0 0 1 0
-0.7177610 0.2368733 -0.4479923 -0.4323849 No 0 0 0 1 1 0 1 1
-0.7177610 0.2368733 0.4520231 0.2946915 No 0 0 0 1 1 0 1 1
1.3084542 2.2220335 0.6960869 0.5674086 No 0 0 1 1 0 1 0 1
2.3215618 0.2368733 0.2661716 0.6353679 No 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 0.0751132 -0.4706689 No 0 0 0 1 1 1 0 1
-0.7177610 0.2368733 -0.7299207 -1.2579024 No 0 1 0 1 0 0 1 1
0.2953466 0.2368733 -1.4883320 -1.2220224 No 0 0 0 1 1 0 0 0
0.2953466 0.2368733 0.2134666 -0.6255580 No 0 0 0 1 1 0 0 1
0.2953466 -4.3289950 0.5330423 0.0254388 No 0 0 0 1 0 0 0 1
1.3084542 0.2368733 -0.4108490 -0.6590866 No 0 0 0 0 1 1 0 0
1.3084542 -1.7482868 0.8752914 0.5361694 No 0 0 0 0 0 1 0 0
1.3084542 0.2368733 0.6960869 0.6299185 No 0 0 1 0 0 0 0 1
2.3215618 -2.7408669 1.9183731 -3.5072290 No 1 1 0 1 1 0 1 1
-0.7177610 -2.7408669 -0.3209729 -0.8520528 No 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 0.0316177 0.1280076 No 1 1 0 0 1 1 0 0
-0.7177610 0.2368733 0.5999048 0.4975528 Yes 0 1 0 1 0 0 1 0
-0.7177610 0.2368733 -1.3347121 -0.5139802 Yes 0 0 0 0 0 0 1 0
-0.7177610 0.2368733 -0.1689506 -0.7407231 Yes 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 0.1455167 0.5361694 Yes 0 1 0 1 0 0 1 0
-0.7177610 0.2368733 -0.6244915 -0.8949495 Yes 0 0 1 1 0 0 1 1
1.3084542 0.2368733 0.4871586 -0.0758578 Yes 0 0 0 1 0 0 1 1
1.3084542 0.2368733 -1.2199751 -0.4162014 Yes 0 0 0 1 0 0 1 1
1.3084542 0.2368733 0.8271417 -0.4775555 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 -0.0893491 0.2447172 Yes 0 1 0 1 0 0 1 0
0.2953466 -1.7482868 -0.5244713 -0.2394390 Yes 0 1 1 1 0 0 1 0
-0.7177610 0.2368733 -0.2519404 -0.7307845 Yes 0 0 0 1 0 0 1 1
0.2953466 0.2368733 1.7129237 0.5247638 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 0.7373576 1.2419205 Yes 0 0 0 0 0 1 0 0
-0.7177610 0.2368733 -0.1367190 -0.6190487 Yes 0 0 0 1 0 1 0 1
1.3084542 0.2368733 -0.1689506 -0.1759075 Yes 1 1 0 1 0 1 0 1
-0.7177610 0.2368733 0.1865701 -0.2098172 Yes 1 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.6645504 -0.0165239 Yes 0 1 0 1 0 0 1 1
-0.7177610 0.2368733 -1.9967392 -1.2879043 Yes 0 1 0 1 0 0 1 1
-0.7177610 0.2368733 -1.0854414 -0.8520528 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -0.6040728 0.1645873 Yes 0 0 1 0 0 0 1 0
-0.7177610 0.2368733 -0.7737420 -0.3199028 Yes 1 1 0 1 0 0 1 0
0.2953466 0.2368733 -2.1253550 0.4448411 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 0.1865701 0.4902826 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 0.1865701 -0.0781287 Yes 1 1 0 1 0 1 0 1
-0.7177610 0.2368733 -0.5244713 -1.1838259 Yes 1 0 1 1 0 1 0 1
-0.7177610 0.2368733 -0.1689506 -0.4711597 Yes 1 1 0 1 0 1 0 0
-0.7177610 0.2368733 -0.6040728 -1.0314467 Yes 0 0 0 1 0 1 0 1
0.2953466 0.2368733 0.6216923 0.9870961 Yes 0 0 0 1 0 0 1 1
1.3084542 0.2368733 0.1865701 -0.3436354 Yes 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 -0.1689506 -0.7902057 Yes 0 0 0 1 0 0 1 1
-0.7177610 -0.7557067 -0.9353700 -0.2804349 Yes 0 1 0 1 0 1 0 0
-0.7177610 0.2368733 -0.3209729 -0.1376105 Yes 1 1 0 1 0 1 0 0
0.2953466 -1.7482868 -1.8760843 -0.0826807 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 0.0751132 -0.8736695 Yes 0 1 0 1 0 1 0 1
1.3084542 0.2368733 -0.5440691 -0.3545057 Yes 0 0 1 1 0 1 0 1
-0.7177610 0.2368733 -0.4479923 -0.7902057 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.5667601 0.4571282 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.0020722 -0.5353407 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.1367190 -0.0285728 Yes 0 1 0 1 0 1 0 0
-0.7177610 -3.7334470 -3.2276973 -0.2293611 Yes 0 1 1 1 0 1 0 0
-0.7177610 0.2368733 -1.8760843 -1.1458023 Yes 0 0 0 1 0 1 0 1
-0.7177610 -2.7408669 -0.2519404 -0.0085443 Yes 0 0 1 1 0 1 0 0
-0.7177610 0.2368733 0.0020722 -1.3228128 Yes 0 0 0 1 0 1 0 1
1.3084542 -2.7408669 0.0316177 0.0272064 Yes 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 0.2791282 0.2116086 Yes 0 1 0 1 0 1 0 1
-0.7177610 0.2368733 0.3920195 -0.1494746 Yes 0 0 0 1 0 0 1 1
0.2953466 0.2368733 -0.5244713 -0.4879444 Yes 0 0 0 1 0 0 1 1
1.3084542 0.2368733 1.0568145 1.5108650 Yes 0 0 0 1 0 0 1 1
2.3215618 2.2220335 -0.6451262 -1.0754269 Yes 0 0 1 1 0 1 0 1
-0.7177610 0.2368733 0.0751132 0.3527908 Yes 0 1 0 1 0 0 1 0
-0.7177610 0.2368733 0.1176596 -0.5502674 Yes 1 0 0 1 0 1 0 1
0.2953466 0.2368733 -0.0128701 0.8643113 Yes 1 1 0 0 0 1 0 0
-0.7177610 0.2368733 -0.2017239 0.2750192 Yes 0 1 0 1 0 1 0 0
0.2953466 -2.7408669 0.6751194 1.8816769 Yes 0 0 0 1 0 0 0 0
-0.7177610 0.2368733 0.3174894 -0.4319064 Yes 1 0 0 1 0 1 0 1
0.2953466 0.2368733 -0.8413776 -0.9742684 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 1.3312286 1.3706797 Yes 1 1 0 1 0 0 1 0
-0.7177610 0.2368733 0.6216923 0.4496609 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 -2.1253550 -0.0165239 Yes 1 1 0 1 0 1 0 0
-0.7177610 0.2368733 0.0893987 -1.0278978 Yes 1 1 0 1 1 1 0 1
1.3084542 0.2368733 -0.9353700 -0.5358530 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 0.7677473 0.0314404 Yes 0 0 1 1 0 0 0 1
2.3215618 0.2368733 2.0267323 2.6239813 Yes 0 0 0 1 0 0 0 0
-0.7177610 0.2368733 -0.5244713 -0.1301443 Yes 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 -1.1116158 -1.1177099 Yes 0 1 0 1 0 0 1 1
-0.7177610 0.2368733 -1.2199751 -0.6635394 Yes 0 1 0 1 0 1 0 0
-0.7177610 -3.7334470 -3.2276973 -0.2982873 Yes 0 0 0 0 0 1 0 0
-0.7177610 0.2368733 -0.4858569 0.2277892 Yes 0 1 0 1 0 1 0 0
-0.7177610 0.2368733 0.4871586 0.3813055 Yes 0 1 0 1 0 0 1 0
1.3084542 -2.7408669 0.6960869 1.5553245 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -0.5244713 -0.8968989 Yes 1 0 0 1 0 1 0 1
1.3084542 0.2368733 -1.2199751 1.9107965 Yes 1 0 0 1 0 0 1 0
0.2953466 0.2368733 -2.8721766 -1.5262267 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 0.2919993 -0.7442464 Yes 0 0 0 0 0 0 0 1
2.3215618 -2.7408669 2.9693976 3.4103549 Yes 0 0 0 1 1 1 0 0
0.2953466 0.2368733 0.3920195 0.5361694 Yes 0 0 0 1 0 0 0 0
-0.7177610 0.2368733 -0.1689506 -0.5241143 Yes 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 -0.5638659 -1.2357846 Yes 0 0 1 1 0 1 0 1
1.3084542 0.2368733 0.3674913 -0.0161603 Yes 0 0 0 1 0 0 0 1
-0.7177610 -2.7408669 -1.0854414 1.2038879 Yes 0 0 0 1 0 0 1 0
1.3084542 0.2368733 0.8271417 0.9720209 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 -0.6244915 -1.2711633 Yes 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 -0.2350575 -0.2817340 Yes 0 0 0 1 0 0 0 1
-0.7177610 -2.7408669 -1.1381464 0.1336170 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 -0.3744000 -0.2272699 Yes 1 0 0 1 0 1 0 0
-0.7177610 0.2368733 -1.9556858 0.2246909 Yes 0 1 1 1 0 0 0 0
-0.7177610 0.2368733 -0.2861512 -0.3172394 Yes 1 0 0 1 0 1 0 1
0.2953466 0.2368733 -0.7299207 -0.7307845 Yes 0 0 1 1 0 1 0 1
1.3084542 0.2368733 0.4638054 0.2364307 Yes 0 1 0 1 1 1 0 0
0.2953466 0.2368733 0.0607222 -0.2804349 Yes 0 0 1 1 0 0 1 1
1.3084542 0.2368733 -0.1050116 -0.1098966 Yes 0 0 1 1 0 1 0 0
-0.7177610 0.2368733 -0.1689506 0.0690227 Yes 1 1 1 1 0 1 0 0
-0.7177610 0.2368733 -0.9595935 -0.3955114 Yes 1 1 0 1 0 0 1 0
-0.7177610 0.2368733 -1.6902328 -0.0876275 Yes 0 1 1 1 0 0 0 1
-0.7177610 0.2368733 -1.5533369 -0.8029858 Yes 0 1 0 1 0 0 1 0
0.2953466 0.2368733 1.1826624 0.9311361 Yes 0 0 0 0 0 0 1 0
2.3215618 0.2368733 -0.0128701 0.4592549 Yes 0 0 1 0 0 0 0 0
-0.7177610 0.2368733 0.0893987 -0.4290388 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 0.0020722 -0.1852284 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.1865701 -0.5286969 Yes 0 1 0 1 0 1 0 1
-0.7177610 0.2368733 0.5667601 0.8899172 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.0431027 0.5980166 Yes 0 0 0 1 0 1 0 0
1.3084542 0.2368733 0.9222808 0.5980166 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.8271417 0.6339482 Yes 0 1 0 0 0 0 0 0
0.2953466 0.2368733 -0.4479923 -0.7956697 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.1689506 -0.4214183 Yes 0 1 1 1 0 0 0 1
-0.7177610 0.2368733 1.4832509 1.0338075 Yes 1 1 0 1 0 1 0 0
0.2953466 0.2368733 0.1316379 -0.4376579 Yes 0 0 0 1 0 1 0 1
0.2953466 0.2368733 0.5102354 0.4043432 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 -0.1367190 -0.2965363 Yes 1 1 0 0 0 0 1 0
0.2953466 0.2368733 -0.6040728 -0.3627099 Yes 0 0 0 1 0 1 0 0
-0.7177610 -4.7260271 0.3301106 -0.5317596 Yes 0 1 0 1 0 0 1 1
1.3084542 0.2368733 -0.3744000 0.5980166 Yes 0 0 0 0 0 0 0 1
-0.7177610 0.2368733 -0.1208009 -0.3216810 Yes 0 0 1 1 0 1 0 1
1.3084542 0.2368733 -0.3034843 1.2644150 Yes 0 0 0 1 0 0 1 0
0.2953466 -2.7408669 0.0893987 0.6824040 Yes 0 0 1 1 0 0 1 0
0.2953466 2.2220335 -0.1208009 -0.5747756 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.7299207 -1.2803480 Yes 0 0 0 1 0 0 1 1
0.2953466 -2.7408669 1.4762742 -0.6956497 Yes 0 0 1 1 0 1 0 1
2.3215618 0.2368733 1.3008783 -0.4726333 Yes 1 1 0 1 0 0 0 0
0.2953466 0.2368733 1.8036361 0.5569839 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 0.0462243 -1.0834954 Yes 0 1 0 1 0 1 0 1
-0.7177610 0.2368733 -0.1689506 0.8123949 Yes 1 1 0 1 0 0 0 0
-0.7177610 0.2368733 -0.0431027 0.3094265 Yes 0 1 0 0 0 1 0 0
1.3084542 0.2368733 0.2661716 0.1687765 Yes 0 0 0 1 0 1 0 1
2.3215618 0.2368733 0.3920195 0.0430231 Yes 0 0 0 1 1 0 1 1
0.2953466 0.2368733 -0.7299207 -0.5779331 Yes 1 1 0 1 0 0 1 1
-0.7177610 0.2368733 -2.8721766 -0.3987842 Yes 1 1 0 1 0 0 1 0
-0.7177610 0.2368733 -0.0738115 -0.6007560 Yes 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 0.2661716 -0.7191749 Yes 0 0 0 1 0 1 0 1
0.2953466 0.2368733 -0.2519404 -0.6458064 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.8942233 -0.0807824 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.9595935 -0.4628366 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 2.3467982 1.8816769 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 -0.3034843 -0.1060337 Yes 0 0 0 1 0 1 0 1
1.3084542 -2.7408669 -1.0089624 -0.9158740 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 -0.7517082 -0.4362180 Yes 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 -0.1689506 -0.1946070 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 -0.2519404 -0.0318756 Yes 1 1 0 1 0 1 0 0
-0.7177610 0.2368733 0.6960869 1.2336572 Yes 0 1 0 1 0 0 0 0
-0.7177610 0.2368733 0.1176596 -0.4765697 Yes 0 0 0 1 0 0 0 1
1.3084542 0.2368733 0.0462243 -0.4386186 Yes 0 0 0 1 0 0 1 1
0.2953466 0.2368733 0.0607222 0.1552002 Yes 1 0 0 1 0 0 1 0
-0.7177610 0.2368733 -0.4293322 -0.5784599 Yes 0 0 1 1 0 1 0 1
-0.7177610 0.2368733 0.0169009 0.2599440 Yes 1 1 0 1 0 0 0 0
-0.7177610 0.2368733 0.1316379 -0.0781287 Yes 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 0.0607222 -0.1710671 Yes 1 1 0 1 0 0 0 1
1.3084542 0.2368733 0.6751194 0.9076070 Yes 0 0 1 1 0 0 0 0
-0.7177610 0.2368733 -0.8644544 -0.5564872 Yes 1 1 0 1 0 1 0 0
-0.7177610 0.2368733 -1.6550973 -0.7859695 Yes 1 1 0 1 0 0 0 0
1.3084542 -4.3289950 0.1865701 0.4706354 Yes 0 0 0 0 0 0 0 0
1.3084542 0.2368733 -0.1689506 -0.1892989 Yes 0 0 0 1 0 1 0 0
0.2953466 0.2368733 -0.3034843 -0.2627903 Yes 1 1 0 1 0 1 0 0
1.3084542 0.2368733 -0.4293322 -0.2373339 Yes 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 0.0169009 -0.4323849 Yes 1 1 0 1 0 0 0 1
-0.7177610 0.2368733 -0.0128701 0.2345831 Yes 0 1 0 1 1 0 0 0
0.2953466 0.2368733 -0.2183195 0.0607513 Yes 0 1 0 1 0 0 1 1
-0.7177610 0.2368733 -0.5638659 -0.5471670 Yes 0 1 0 1 0 1 0 0
1.3084542 2.2220335 -1.2199751 0.4051694 Yes 0 0 0 0 0 1 0 1
1.3084542 0.2368733 -0.0893491 -0.7407231 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 0.3920195 -0.7908118 Yes 0 0 0 1 0 1 0 1
1.3084542 0.2368733 -0.0738115 -0.4726333 Yes 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 1.3312286 0.6180040 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 1.0826422 1.1643756 Yes 0 0 0 1 0 1 0 0
2.3215618 0.2368733 -0.6244915 -0.6928057 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 -0.0128701 -0.8376028 Yes 1 0 0 1 0 1 0 1
-0.7177610 -2.7408669 -0.2350575 -0.1506662 Yes 1 1 0 1 0 0 1 0
1.3084542 0.2368733 0.4520231 1.0338075 Yes 0 0 0 1 0 0 0 1
2.3215618 0.2368733 0.8271417 0.3097198 Yes 0 0 0 1 0 1 0 1
1.3084542 0.2368733 1.1498891 0.2599440 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.0128701 0.1822323 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 0.1455167 0.7306910 Yes 0 1 0 0 0 0 0 0
0.2953466 0.2368733 0.5667601 0.6160889 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 -1.6902328 1.9991765 Yes 1 1 0 0 0 1 0 0
1.3084542 -0.7557067 2.0787345 1.2038879 Yes 0 0 0 0 0 0 0 1
-0.7177610 0.2368733 -0.3386198 -0.6381130 Yes 1 1 0 1 0 0 1 1
-0.7177610 0.2368733 -0.0128701 0.0349597 Yes 0 0 0 1 0 1 0 0
1.3084542 0.2368733 -0.0128701 0.3740163 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -0.0431027 -0.3545057 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.3426506 0.5361694 Yes 1 1 0 1 0 0 1 0
0.2953466 -2.7408669 -0.2861512 -0.2098172 Yes 0 1 0 1 0 0 1 0
-0.7177610 0.2368733 0.0751132 -0.8949495 Yes 0 0 0 1 0 0 1 1
2.3215618 0.2368733 0.6751194 1.1783224 Yes 0 1 0 1 0 0 0 0
2.3215618 0.2368733 1.4481154 1.2330197 Yes 0 0 0 1 0 0 0 0
-0.7177610 0.2368733 -0.1689506 -0.7407231 Yes 0 0 0 1 0 0 1 1
1.3084542 -2.7408669 -0.2861512 -1.1335903 Yes 0 0 1 1 0 1 0 1
1.3084542 0.2368733 -0.2861512 -0.4643021 Yes 0 0 0 1 0 0 1 1
1.3084542 0.2368733 0.0607222 -0.3806397 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.6244915 -0.4974057 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 -0.4668327 -0.7902057 Yes 0 1 0 1 0 1 0 1
-0.7177610 0.2368733 -1.7624620 -1.1565692 Yes 1 1 1 1 0 1 0 0
-0.7177610 0.2368733 -0.2519404 -1.0067769 Yes 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 0.3920195 0.5994704 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -0.3386198 -0.9478129 Yes 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 -1.5205636 -0.5165074 Yes 1 1 0 1 0 0 1 0
-0.7177610 0.2368733 -0.3034843 -2.1410175 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 0.1035803 -0.3945776 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.1035803 0.4923634 Yes 0 0 0 1 0 0 0 0
-0.7177610 0.2368733 -0.5244713 -1.2711633 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 -0.6040728 1.9122226 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 -0.1527682 -0.6928057 Yes 0 0 0 1 0 0 0 1
1.3084542 0.2368733 0.0316177 0.5196669 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 -0.7960275 -0.5549298 Yes 0 0 0 1 0 0 0 1
0.2953466 0.2368733 0.2661716 1.0858121 Yes 1 1 0 0 0 1 0 0
-0.7177610 0.2368733 2.2029781 1.6930668 Yes 0 1 0 1 0 1 0 0
-0.7177610 0.2368733 0.1729814 -0.8363527 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.5244713 -0.1759075 Yes 0 1 0 1 0 0 1 0
-0.7177610 -1.7482868 0.2791282 1.3171976 Yes 1 0 0 1 0 1 0 0
-0.7177610 0.2368733 -1.8760843 -1.1853984 Yes 0 1 0 1 0 1 0 1
-0.7177610 0.2368733 -0.1689506 -0.9749528 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 0.1035803 0.8001410 Yes 1 0 1 0 0 0 0 0
-0.7177610 0.2368733 0.6216923 0.9531552 Yes 0 0 0 1 0 0 0 1
2.3215618 0.2368733 0.2399972 0.1684547 Yes 0 1 1 1 0 1 0 1
0.2953466 0.2368733 0.2919993 3.3214361 Yes 0 1 0 1 0 1 0 0
-0.7177610 -0.7557067 0.5667601 0.8401258 Yes 0 0 0 1 0 0 0 1
1.3084542 0.2368733 -0.3386198 0.1506475 Yes 0 0 1 1 0 0 0 0
-0.7177610 -2.7408669 -0.7299207 -0.8005432 Yes 0 0 0 1 1 0 0 1
-0.7177610 0.2368733 -0.8644544 0.7880009 Yes 0 1 0 0 0 1 0 0
1.3084542 0.2368733 0.0751132 0.0503481 Yes 0 1 0 1 0 0 0 0
-0.7177610 0.2368733 0.3920195 -1.0666736 Yes 0 0 0 0 0 1 0 1
-0.7177610 0.2368733 0.3047860 -0.5064238 Yes 0 0 0 1 0 0 0 1
0.2953466 0.2368733 0.0169009 0.3167407 Yes 1 1 1 1 0 1 0 0
-0.7177610 0.2368733 0.1316379 0.5361694 Yes 0 1 0 1 0 0 0 0
0.2953466 0.2368733 0.4162429 -0.3022345 Yes 0 0 0 0 0 1 0 1
2.3215618 0.2368733 -0.5244713 0.1632960 Yes 0 0 0 1 0 1 0 0
-0.7177610 0.2368733 -0.6659817 -0.7902057 Yes 1 1 0 0 0 0 1 0
1.3084542 0.2368733 0.4162429 0.4131325 Yes 0 1 0 1 0 0 0 1
1.3084542 0.2368733 0.2661716 2.0710848 Yes 0 0 0 0 0 0 0 1
0.2953466 0.2368733 -0.8185707 2.0840480 Yes 0 0 0 0 0 1 0 1
-0.7177610 0.2368733 0.3174894 0.0489556 Yes 1 1 1 1 0 0 0 0
2.3215618 0.2368733 -0.2861512 -0.4667477 Yes 0 0 1 1 0 0 0 0
-0.7177610 0.2368733 1.1332935 1.4313153 Yes 0 0 0 1 0 1 0 0
1.3084542 0.2368733 1.2308121 1.0338075 Yes 0 0 0 1 0 1 0 0
1.3084542 0.2368733 0.3920195 0.1280076 Yes 0 0 0 0 0 1 0 1
-0.7177610 0.2368733 -0.6040728 -0.6741674 Yes 0 1 1 1 0 1 0 1
0.2953466 0.2368733 0.6856313 0.3576281 Yes 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 -0.3386198 -0.1999337 Yes 0 1 1 1 0 0 0 0
-0.7177610 0.2368733 1.0568145 2.1372788 Yes 1 1 1 0 0 1 0 0
0.2953466 -4.3289950 -0.4293322 -0.3576910 Yes 1 0 0 1 0 1 0 1
-0.7177610 -5.5200911 -0.3209729 0.1418287 Yes 0 0 0 1 0 0 1 1
-0.7177610 2.2220335 -0.2861512 -2.8427830 Yes 1 1 0 1 0 0 0 1
1.3084542 0.2368733 0.9036207 0.2184753 Yes 0 0 0 1 0 1 0 1
-0.7177610 -0.7557067 -0.1050116 -0.1502689 Yes 0 0 1 1 0 1 0 1
1.3084542 0.2368733 1.2068859 -0.2761123 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 2.5342754 2.4220661 Yes 0 0 0 1 0 0 1 1
-0.7177610 0.2368733 0.7064868 -0.8029858 Yes 0 0 0 0 0 0 1 1
0.2953466 0.2368733 1.2622639 0.5524942 Yes 0 0 0 1 0 1 0 1
2.3215618 0.2368733 0.2661716 -0.2952244 Yes 0 0 0 1 0 0 0 1
1.3084542 2.2220335 -0.2861512 -1.1049752 Yes 1 0 0 1 0 1 0 1
-0.7177610 0.2368733 -1.6902328 -0.7902057 Yes 0 1 0 1 0 1 0 0
2.3215618 0.2368733 -0.5244713 0.4523320 Yes 0 0 0 0 0 0 0 1
1.3084542 0.2368733 2.5342754 0.9621410 Yes 0 0 0 0 0 0 0 1
0.2953466 0.2368733 0.4041688 0.3439301 Yes 0 0 1 1 1 1 0 1
-0.7177610 0.2368733 -0.0128701 0.7220049 Yes 0 1 1 1 0 1 0 0
2.3215618 0.2368733 -0.5244713 0.0247312 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -0.0893491 2.2144418 Yes 1 1 1 0 0 0 1 0
0.2953466 -2.7408669 0.1316379 0.1362496 Yes 1 0 0 1 0 1 0 1
0.2953466 0.2368733 -0.6244915 -0.2160272 Yes 1 1 0 1 0 1 0 0
0.2953466 0.2368733 -0.0431027 -0.9572567 Yes 0 0 1 1 0 0 1 1
-0.7177610 0.2368733 -0.4858569 -0.5054191 Yes 1 0 1 1 0 1 0 1
-0.7177610 0.2368733 0.3301106 -0.6359220 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 0.2000647 -0.7745305 Yes 0 1 0 1 0 0 0 1
-0.7177610 0.2368733 -0.3386198 -0.6266457 Yes 0 0 0 1 0 0 0 1
0.2953466 0.2368733 -0.1689506 -0.7950616 Yes 0 0 1 1 0 0 0 1
0.2953466 0.2368733 0.3674913 -0.3545057 Yes 0 0 0 1 0 0 1 1
0.2953466 0.2368733 -3.1512183 0.3955027 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 -0.8644544 -0.6922375 Yes 1 1 0 1 0 0 1 1
2.3215618 0.2368733 1.3387429 1.3310077 Yes 0 0 0 0 0 1 0 0
-0.7177610 0.2368733 0.4162429 -0.1215449 Yes 0 0 0 1 0 1 0 1
-0.7177610 0.2368733 0.6432391 -0.0114411 Yes 1 0 0 1 0 1 0 1
1.3084542 0.2368733 -0.3744000 -0.2065155 Yes 0 0 1 1 0 1 0 1
0.2953466 0.2368733 2.9693976 2.3206419 Yes 1 0 0 0 0 1 0 0
-0.7177610 0.2368733 0.9315443 0.5166005 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 0.0169009 0.4848070 Yes 0 1 1 0 0 1 0 0
-0.7177610 0.2368733 -0.1689506 0.0891667 Yes 0 0 1 1 0 0 0 0
-0.7177610 0.2368733 -1.2199751 -0.3545057 Yes 0 1 0 1 0 0 1 0
1.3084542 0.2368733 1.4481154 2.0839571 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 -0.3386198 -0.4025331 Yes 0 1 0 1 0 0 1 1
-0.7177610 0.2368733 -1.2199751 -1.0081758 Yes 1 1 1 1 0 1 0 0
-0.7177610 0.2368733 -0.6451262 0.1822323 Yes 0 1 0 1 0 1 0 0
-0.7177610 -2.7408669 -1.6550973 -0.5139802 Yes 0 0 0 1 0 1 0 1
2.3215618 0.2368733 1.5650796 1.2094031 Yes 0 1 0 0 0 1 0 0
-0.7177610 -2.7408669 0.1592974 -0.1324981 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 1.3387429 2.0334910 Yes 0 0 0 0 0 0 1 0
1.3084542 0.2368733 0.7475402 0.4795737 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 0.3301106 -0.5684804 Yes 0 0 1 0 0 0 0 1
2.3215618 0.2368733 -0.0431027 0.4592549 Yes 0 0 0 1 0 0 1 0
-0.7177610 0.2368733 0.5330423 -0.2061033 Yes 0 1 0 1 0 0 0 1
0.2953466 0.2368733 2.5982144 1.5863190 Yes 1 0 0 1 0 1 0 0
0.2953466 0.2368733 0.5443464 -0.3243523 Yes 0 0 0 1 0 1 0 1
1.3084542 0.2368733 0.3551105 -0.0830606 Yes 0 0 1 1 0 0 0 1
-0.7177610 0.2368733 -0.3744000 -0.4011262 Yes 0 0 0 1 0 0 0 1
-0.7177610 0.2368733 -1.1923156 -0.5653426 Yes 1 1 0 1 0 0 0 0
2.3215618 -2.7408669 -2.3112065 -0.0385028 Yes 0 0 0 1 0 0 0 0
1.3084542 0.2368733 0.6960869 0.8909165 Yes 0 0 0 1 0 0 1 0


Notice that, by dummy-coding the nomial predictors, we’ve increased the number of columns in our dataset. This is because each factor has been transformed into k-1 dummy variables, with one level held out as the reference (or baseline) level. The baseline level is not visible in our dataset and assigned a value of 0. For a given predictor, if the dummy variables corresponding to every other level is 0, then we default to the baseline. For instance both Property_Area_Urban and Property_Area_Semiurban are 0, then the applicant must be from a Rural area.

K-fold cross-validation

We will stratify on our response variable Loan_Status and use 10 folds to perform stratified cross validation. K-fold cross-validation divides our data into k folds of roughly equal sizes, holds out the first fold as a validation set, and fits the model on the remaining k-1 folds as if they were the training set. This is repeated k times; each time, a different fold is used as a validation set. This results in k estimates of the test MSE (or in the classification case, test error rate).

loan_folds <- vfold_cv(loan_train, v = 10, strata = Loan_Status)

To save computational time, we will save the results to an RDA file; once we have the model we want, we can go and load it later with no time commitment.

save(loan_ds, loan_folds, loan_recipe, loan_train, loan_test, 
     file = "~/Desktop/School/PSTAT/PSTAT 131/proj-final/rda_files/loan-setup.rda")

Model Building

It’s time to build our models! For ease of efficiency and access, I will be building each model in a separate R file and saving my results in RDA files. The models will then be loaded below for further exploration. This allows us to streamline our analysis and save on computational time.

For each model, we will:

  1. Set up the model by specifying its type, engine, and mode.
  2. Set up the workflow; add the model and defined recipe.

For models requiring parameter tuning, we’ll complete steps 3-5.

  1. Use grid_regular to set up tuning grids of values for the parameters we’re tuning and specify levels for each.
  2. Fit the models to our folded data via tune_grid().
  3. Select the best value(s) of the parameter(s) based on roc_auc and finalize the workflow.
  4. Fit the final model to entire training set.
  5. Save results to RDA file.

Afterwards, we’ll load back in the saved files, collect error metrics, and analyze their individual performances.

Error metric

The performance metric we’ll be using is roc_auc, which stands for area under the ROC curve. The ROC (receiver operating characteristics) curve is a popular graphic that plots true positive rate (TPR) vs. false positive rate (FPR) at various threshold settings. TPR is sensitivity (proportion of observations that are correctly classified), while FPR is 1-specificity (proportion of observations that are incorrectly classified); the higher the TPR, the beter. The AUC (area under curve) is a measure of the diagostic ability of a classifier, highlighting the trade-off between sensitivity and specificity.

Model Evaluation

It’s time to load our models back in to evaluate their results!

load(file= "~/Desktop/School/PSTAT/PSTAT 131/proj-final/rda_files/logistic.rda")
load(file= "~/Desktop/School/PSTAT/PSTAT 131/proj-final/rda_files/knn.rda")
load(file= "~/Desktop/School/PSTAT/PSTAT 131/proj-final/rda_files/en.rda")
load(file= "~/Desktop/School/PSTAT/PSTAT 131/proj-final/rda_files/lda.rda")
load(file= "~/Desktop/School/PSTAT/PSTAT 131/proj-final/rda_files/qda.rda")
load(file= "~/Desktop/School/PSTAT/PSTAT 131/proj-final/rda_files/decision-tree.rda")

Model autoplots

Here, we will visualize the results of our tuned models. We will use the autoplot function to visualize the effect of varying select parameters on the performance of each model according to its impact on our metric of choice.

K-nearest neighbors

For the KNN model, we had 10 different levels of neighbors. In general, the higher the number of neighbors, the greater the roc_auc. The roc_auc score of the best performing model (k=10) is approxmiately 0.71, which is pretty decent.

autoplot(knn_tune_res)

Elastic net

In our elastic net model, we tuned 2 parameters with 10 levels of each: penalty, the amount of regularization, and mixture, the proportion of lasso penalty (1 for pure lasso, 0 for pure ridge). We can see from the graph that the optimal mixture was 0 (ie., pure ridge model), lower levels of mixture resulted in higher roc_auc scores, and that models performed worse as penalty increased.

autoplot(en_tune_res)

Decision tree

For our decision tree model, we focused on the parameter cost_complexity and tuned it with 10 levels. Oftentimes decision trees can have too many splits, leading to a very complex model that is likely to overfit the data. A smaller tree with fewer splits can address this issue by yielding a simpler model (better interpretation, more bias).

The idea of cost-complexity pruning is similar to that of lasso / ridge regularization; first, we grow a very large tree, then consider a sequenced of pruned subtrees and select the one that minimizes a penalized error metric. The tuning parameter cost_complexity controls a trade-off between a subtree’s complexity and its fit to the training data; when cost_complexity is 0, it’s the same as the the training error rate; as cost_complexity increases, the tree is penalized for having too many nodes.

We can see from the plot below that a cost-complexity of about 0.001 yields the optimal model. This indicates that our tree does not require pruning / penalization after all. Note that the parameter uses the log10_trans() functions by default, so all of the values in our grid are in the log10 scale.

autoplot(dt_tune_res)

Model selection

Here, we will compare the performance of each model on the training data and create visualization. I’ve created a tibble in order to display the estimated testing roc_auc scores for each fitted model.

log_auc <- augment(log_fit, new_data = loan_train) %>%
  roc_auc(truth = Loan_Status, .pred_Yes) %>%
  select(.estimate)

lda_auc <- augment(lda_fit, new_data = loan_train) %>%
  roc_auc(truth = Loan_Status, .pred_Yes) %>%
  select(.estimate)

qda_auc <- augment(qda_fit, new_data = loan_train) %>%
  roc_auc(truth = Loan_Status, .pred_Yes) %>%
  select(.estimate)

knn_auc <- augment(knn_final_fit, new_data = loan_train) %>%
  roc_auc(truth = Loan_Status, .pred_Yes) %>%
  select(.estimate)

en_auc <- augment(en_final_fit, new_data = loan_train) %>%
  roc_auc(truth = Loan_Status, .pred_Yes) %>%
  select(.estimate)

dt_auc <- augment(dt_final_fit, new_data = loan_train) %>%
  roc_auc(truth = Loan_Status, .pred_Yes) %>%
  select(.estimate)



roc_aucs <- c(log_auc$.estimate,
                           lda_auc$.estimate,
                           qda_auc$.estimate,
                           knn_auc$.estimate,
                           en_auc$.estimate,
                           dt_auc$.estimate)

mod_names <- c("Logistic Regression",
            "LDA",
            "QDA",
            "KNN",
            "Elastic Net",
            "Decision Tree")
mod_results <- tibble(Model = mod_names,
                             ROC_AUC = roc_aucs)

mod_results <- mod_results %>% 
  dplyr::arrange(-roc_aucs)

mod_results

While all of our models did relatively well, the best-performing model is the KNN model with an roc_auc score of 0.92, with the decision tree close behind at approximately 0.87 I’ve created a lollipop plot below to help visualize these results.

lp_plot <- ggplot(mod_results, aes(x = Model, y = ROC_AUC)) + 
    geom_segment( aes(x = Model, xend = 0, y = ROC_AUC, yend = 0)) +
  geom_point( size=7, color= "black", fill = alpha("blue", 0.3), alpha=0.7, shape=21, stroke=3) +
  labs(title = "Model Results") + 
  theme_minimal()

lp_plot

Results of the Best Models

Now that we’ve identified our best models, we can continue to further analyze their true performance. We will start with the KNN model and also analyze the performance of the decision tree and QDA model as a means of comparison.

KNN model

Performance on the folds

So, the KNN model performed the best overall, but which of value of neighbors yields the best performance?

# select metrics of best knn model
knn_tune_res %>% 
  collect_metrics() %>% 
  dplyr::arrange(mean) %>% 
  slice(10)

KNN model #10 with 11 predictors, 10 neighbors, and a mean roc_auc score of 0.703 performed the best! Now that we have our best model, we can fit it to our testing data to explore its true predictive power.

Testing the model

The KNN model did a pretty decent job in prediction on the test set. In general, an AUC value between 0.7-0.8 is considered acceptable. Given the complex nature of our problem, I would classify this as a win.

knn10_roc_auc <- augment(knn_final_fit, new_data = loan_test) %>%
  roc_auc(Loan_Status, .pred_Yes)  %>%
  select(.estimate)

knn10_roc_auc 

ROC curve

A confusion matrix of the test results indicate a slightly high but acceptable Type I and Type II error probability (both ~ 10%).

knn_test_results <- augment(knn_final_fit, new_data = loan_test)

knn_test_results %>% 
  conf_mat(truth = Loan_Status, estimate = .pred_class) %>% 
  autoplot(type = "heatmap")

knn_test_results %>% 
  roc_curve(Loan_Status, .pred_Yes) %>%
  autoplot()

In general, the more an ROC curve resembles the top left angle of a square, the better the AUC. While our curve is not perfect, it has the correct shape and looks pretty decent.

Here’s a distribution of the predicted probabilities.

knn_test_results %>% 
  ggplot(aes(x = .pred_Yes, fill = Loan_Status)) + 
  geom_histogram(position = "dodge") + theme_bw() +
  xlab("Probability of Yes") +
  scale_fill_manual(values = c("blue", "orange"))

Decision tree

As previously mentioned, we will also explore the results of the decision tree and QDA models on our testing data. We will start with the decision tree. First, let’s compute its roc_auc score and then create visualization as needed.

Testing the model

The decision tree actually performed slightly better than the KNN model in terms of predictive accuracy, albeit by a slight margin.

dt_roc_auc <- augment(dt_final_fit, new_data = loan_test, type = 'prob') %>%
  roc_auc(Loan_Status, .pred_Yes) %>%
  select(.estimate)

dt_roc_auc

ROC curve

The decision tree’s ROC curve illustrates that, as the false positive rate (FPR) increases, the model performs better. In fact, it actually performs great once FPR exceeds 0.5. As FPR increases, specificity falls and, as indicated by the graph, sensitivity increases. For our classifier, sacraficing specitifity to boost sensitivity is the right choice.

dt_roc_curve <- augment(dt_final_fit, new_data = loan_test, type = 'prob') %>%
  roc_curve(Loan_Status, .pred_Yes) %>%
  autoplot()

dt_roc_curve

QDA

Now, it’s time to analyze our quadratic discriminant analysis (QDA) classifier. In short, it’s a more advanced version of a LDA model used to find a non-linear decision boundaries between classifiers, assuming each class follows a Gaussian distribution.

Testing the model

To my surprise, the QDA model performed the best out of our top 3 models! Its computed roc_auc score is only slightly higher than that of the decision tree model. Nevertheless, a 0.01 point increase is very significant when it comes to AUC.

qda_roc_auc <- augment(qda_fit, new_data = loan_test, type = 'prob') %>%
  roc_auc(Loan_Status, .pred_Yes) %>%
  select(.estimate)

qda_roc_auc

ROC curve

To visualize this result, let’s plot a ROC curve:

augment(qda_fit, new_data = loan_test, type = 'prob') %>%
  roc_curve(Loan_Status, .pred_Yes) %>%
  autoplot()

Instead of fluctuating between concavity and convexity (in the case of KNN), or looking flat then curve up after a specific FPR rate (in the case of decision tree), the QDA model’s ROC curve is consistently concave and looks the best out of the 3 models considered.

Conclusion

To summarize, in this project, we tackled the problem of loan prediction given select demographics specified in applicant profiles. We worked with a relatively small dataset (train.csv) with a large number of feature; we tidied the data, performed exploratory analysis, and fit a number of models of varying complexity and flexibility. Through analysis, testing, and assessment, we found the KNN model to be most optimal for predicting the loan status of an applicant. However, the model was not perfect and leaves room for improvment.

In fact, none of our models performed particularly well for this problem. This can be due to a variety of factors, such as a violation of assumptions and overfitting. None of the models considereed were particularly robust in preventing overfitting (with the exception of Ridge). The Logistic model assumes that the data is linearly seperable, and performs poorly when relationships are non-linear. It also tends to overfit in higher-dimensions. The LDA model assumes a linear decision boundary, and is also prone to error in higher dimensions. The Elastic Net (Ridge) model was efficient for variance reduction, but significantly increased bias (it was the worst fit to our training data). The QDA model, while an improvement from LDA/Logistic, was not as flexible as KNN or decision tree. For more complex decision boundaries, a non-parametric approach may be preferred. The decision tree, a simpler model, is highly variant and tends to overfit. KNN doesn’t require linear separability and makes no distributional assumptions; however, it does not model relationships very well and is also prone to overfitting.

Given that the KNN and QDA models outperformed most of our models in testing, and that models with linear decision boundaries performed poorly, we can say that the relationships in our data are likely non-linear. A potential improvement would be to consider more non-linear models or non-linear extensions to some of our models; perhaps even non-parametric approaches.

As far as our error metrics (as measured by roc_auc), the QDA model performed better than the KNN model on the test set, whereas the KNN model perfromed better for the train. It is however important to note that both models did not have high accuracy, likely due to the fact that neither are optimal for dimensionality reduction. A more flexible approach, such as a random forest, may be better suitable for our data. Its removal of redundant features and noise would lead to less misleading data and subsequently an improvement in model accuracy.

It’s also good to acknowledge that none of our models performed particularly poorly in the face of a complex problem. Predicting loan eligibility is no easy feat, and predictive models are undoubetly prone to nuisance factors and noise. In addition, there’s bias in lending. Some attributes like an applicant’s background or motive for applying cannot be quantified. With this understanding, assigning a class label to each applicant based on a select few demographics seems unfair. Instead, applicants should be assessed on a case-by-case basis.